package com.hengpeng.itfin.service.activity.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfin.service.activity.RebateService;
import com.hengpeng.itfinbase.en.RewardRebateStatus;
import com.hengpeng.itfinbase.persist.RewardRebate;

@Lazy
@Service
@Transactional
public class RebateServiceImpl implements RebateService {

    @Autowired
    private ServiceFactory serviceFactory;

    @Override
    public int updateStatus(long userId, RewardRebateStatus oldStatus, RewardRebateStatus newStatus, Date updateTime,
            Date payTime) {
        return serviceFactory.getDaoFactory().getRebateDao()
                .updateStatus(userId, oldStatus, newStatus, updateTime, payTime);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<Object[]> findRewardRebateByStatus(RewardRebateStatus status) {
        DynamicQuery query = new DynamicQuery(RewardRebate.class);
        query.addProjection(Projections.property("userId"));
        query.addProjection(Projections.sum("money", "money"));
        query.eq("status", status);
        query.le("activetime", DateTimeUtil.addDate(DateTimeUtil.getPreviousStartDate(), 1));// 返今天之前的
//        query.isNotNull("activetime");
        query.addGourp("userId");
        query.setQueryType(QueryType.JDBC);
        
        List l = serviceFactory.getPersistService().findByDynamicQuery(null, query);
        return (List<Object[]>) l;
    }

    @Override
    public int activeByReferenceId(String investInfoId, Date d) {
        return serviceFactory.getDaoFactory().getRebateDao().activeByReferenceId(investInfoId, d);
    }

}
